//////////////////////////////////////////////////////////////////////////////////////////
/// \file     ros2_chain_node.h
/// \brief    ros2 底层通信数据链
///
/// \version  V1.0
/// \author   xhome
/// \date     2020/6/8 下午4:56
//////////////////////////////////////////////////////////////////////////////////////////

#ifndef ROS2_CHAIN_NODE_H
#define ROS2_CHAIN_NODE_H

#include <chrono>
#include <memory>
#include <iostream>
#include <string>
#include <string.h>

#include "chain_node_singleton.h"

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std;
using std::placeholders::_1;

class ChainNodePublisher :public rclcpp::Node{
public:
    ChainNodePublisher():Node("ros2_chain_node_pub")
    {

    }

    void create_publisher_node(string topic_name)
    {

        publisher_ = this->create_publisher<std_msgs::msg::String>(topic_name, 20);
    }

    void chain_node_publish(string str)
    {
        std_msgs::msg::String message;

        message.data = str;
        publisher_->publish(message);
        std::cout << "pub:" << str << std::endl;
    }

private:
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
};



class ChainNodeSubsriber :public rclcpp::Node{
public:
    ChainNodeSubsriber():Node("ros2_chain_node_sub")
    {
    }

    void create_subscriber_node(string topic_name)
    {
        subscription_ = this->create_subscription<std_msgs::msg::String>(topic_name, 20,
                std::bind(&ChainNodeSubsriber::topic_call_back, this, _1));
    }

    void topic_call_back(const std_msgs::msg::String::SharedPtr msg)const
    {
        shared_ptr<ChainNodeSingleton> s_intance_ = ChainNodeSingleton::get_instance();
        memcpy(s_intance_->buffer, msg->data.c_str(), msg->data.size());

        std::cout << "msg:" << msg->data.c_str() << std::endl;
    }

public:
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

#endif //ROS2_CHAIN_INTERFACE_ROS2_CHAIN_NODE_H
